//
//  MyEmotionPageView.m
//  WeChat
//
//  Created by Mac on 16/2/27.
//  Copyright © 2016年 Mac. All rights reserved.
//

#import "YJEmotionPageView.h"
#import "YJEmotionBtn.h"
#import "YJEmotion.h"
#import "YJEmotionPopView.h"
//删除键的tag
#define YJEmotionButtonTypeDelete 20
@interface YJEmotionPageView()
@property(nonatomic,weak)UIButton *deletBtn;
@property(nonatomic,strong)YJEmotionPopView *popView;

@end
@implementation YJEmotionPageView
-(YJEmotionPopView *)popView
{
    if (_popView == nil)
    {
        _popView = [YJEmotionPopView popView];
    }
    return _popView;
}

- (instancetype)initWithFrame:(CGRect)frame
{
    self = [super initWithFrame:frame];
    if (self) {
        
        self.backgroundColor = [UIColor clearColor];
        //添加长按手势
        UILongPressGestureRecognizer *recognizer = [[UILongPressGestureRecognizer alloc]initWithTarget:self action:@selector(longPressAction:)];
        [self addGestureRecognizer:recognizer];
        
    }
    return self;
}

/**
 * 长按手势触发事件(按下去,放开,按下去移动都会触发该方法)
 */
- (void)longPressAction:(UILongPressGestureRecognizer *)recognizer
{
    //获取手指的位置(手势对象、touch对象均有该方法: locationInView)
    CGPoint location = [recognizer locationInView:recognizer.view];
    YJEmotionBtn *btn = [self buttonWithPoint:location];
    
    switch (recognizer.state)
    {
    //结束以及被强制取消的时候调用
        case UIGestureRecognizerStateEnded:
        case UIGestureRecognizerStateCancelled:
        {
            [self.popView removeFromSuperview];
             //插入表情
           if (btn != nil)
               [self btnAction:btn];
        }
            break;
    //开始以及移动的时候调用
        case UIGestureRecognizerStateBegan:
        case UIGestureRecognizerStateChanged:
        {
            [self.popView showFrom:btn];
        }
            break;
        default:
            break;
    }
}

-(void)setEmotions:(NSArray *)emotions
{
    _emotions = emotions;
    //添加表情按钮
    for (int k = 0; k < emotions.count; k ++)
    {
        YJEmotionBtn *btn = [[YJEmotionBtn alloc]init];
        btn.emotion = self.emotions[k];
        [btn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
       
        [self addSubview:btn];
    }
    //删除键(这个会是数组self.subviews的最后一个元素)，因为是最后一个添加到self上
    UIButton *deleteBtn = [[UIButton alloc]init];
    [deleteBtn setImage:[UIImage imageNamed:@"emotion_delete_icon"] forState:UIControlStateNormal];
    [deleteBtn addTarget:self action:@selector(btnAction:) forControlEvents:UIControlEventTouchUpInside];
    deleteBtn.tag = YJEmotionButtonTypeDelete;
    [self addSubview:deleteBtn];
    self.deletBtn = deleteBtn;
}

/**
 *  布局表情按钮
 */
-(void)layoutSubviews
{
    [super layoutSubviews];
    //内边距
    CGFloat inset = 10;
    CGFloat btnW = (self.width - 2*inset)/emotionPageViewMaxCol;
    CGFloat btnH = (self.height - inset)/emotionPageViewMaxrow;
    
    for (int i = 0; i < self.subviews.count; i ++)
    {
        YJEmotionBtn *btn = self.subviews[i];
        //略过删除键
        if (btn.tag == YJEmotionButtonTypeDelete)
            continue;
        btn.width  = btnW;
        btn.height = btnH;
        btn.x = inset + (i%emotionPageViewMaxCol)*btnW;
        btn.y = inset + (i/emotionPageViewMaxCol)*btnH;
    }
    //delete
    self.deletBtn.width = btnW;
    self.deletBtn.height = btnH;
    self.deletBtn.x = self.width - inset - self.deletBtn.width;
    self.deletBtn.y = btnH * (emotionPageViewMaxrow - 1) + inset;

}

//点击表情
-(void)btnAction:(YJEmotionBtn *)btn
{
    [[NSNotificationCenter defaultCenter] postNotificationName:YJEmotionButtonDidSelectNotification object:nil userInfo:@{@"keyBoardBtn":btn}];
}

/**
 *  根据位置判断在哪个按钮上面
 *
 *  @param point 手指位置
 *
 *  @return 按钮
 */
- (YJEmotionBtn *)buttonWithPoint:(CGPoint)point
{
    //不遍历删除键
    for (int i = 0; i < self.subviews.count - 1; i ++)
    {
        YJEmotionBtn *btn = self.subviews[i];
        //如果该触摸点在该按钮上
        if (CGRectContainsPoint(btn.frame, point))
            return btn;
    }
    return nil;
}

@end
